import { config } from '../../config/index';
import { getRandomNum } from '../../utils/mock';
import request from '../../utils/request';
import requestAdmin from '../../utils/requestAdmin';

/** 获取商品列表 */
function mockFetchGoodsList(pageIndex = 1, pageSize = 20) {
  const { delay } = require('../_utils/delay');
  const { getGoodsList } = require('../../model/goods');
  return delay().then(() =>
    getGoodsList(pageIndex, pageSize).map((item) => {
      return {
        spuId: item.spuId,
        thumb: item.primaryImage,
        title: item.title,
        price: item.minSalePrice,
        originPrice: item.maxLinePrice,
        tags: item.spuTagList.map((tag) => tag.title),
      };
    }),
  );
}

/** 获取商品列表 */
export function fetchGoodsList(pageIndex = 1, pageSize = 20) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize);
  }
  return new Promise((resolve) => {
    resolve('real api');
  });
}

/** 获取商品列表 */
function mockFetchDataList(pageIndex = 1, pageSize = 20) {
  const { delay } = require('../_utils/delay');
  const { getDataList } = require('../../model/goods');
  return delay().then(() =>
    getDataList(pageIndex, pageSize).map((item, index) => {
      //     type: 1, //1固定价格 , 2竞价
      // status: 1,//1待拍卖 , 2进行中 ，3已结束
      var type = getRandomNum(1, 2);
      var status = getRandomNum(1, 3);
      item.type = type;
      item.status = status;
      item.title = item.title + index;
      return item;
    }),

  );
}

export function fetchDataList(pageIndex = 1, pageSize = 20) {
  // if (config.useMock) {
  //   return mockFetchDataList(pageIndex, pageSize);
  // }
  return new Promise((resolve) => {
    var url = "/car/user/dealer/pageData";
    var data = {
      pageNo: pageIndex,
      pageSize: pageSize,
      orderType: 'desc',
      orderField: 'id'
      // filter:[
      //   {field:'shopId',
      //   operator:'equal',value:}
      // ]
    }
    request.post(url, data, function (res) {
      if (res.code == 0) {
        resolve(res.data);
      } else {
        resolve([]);
      }
    })
  });
}

export function fetchCarList(pageIndex = 1, pageSize = 20) {
  return new Promise((resolve) => {
    var url = "/car/user/stock/pageData";
    var data = {
      pageNo: pageIndex,
      pageSize: pageSize,
      orderType: 'desc',
      orderField: 'id'
      // filter:[
      //   {field:'shopId',
      //   operator:'equal',value:}
      // ]
    }
    request.post(url, data, function (res) {
      if (res.code == 0) {
        resolve(res.data);
      } else {
        resolve([]);
      }
    })
  });
}

export function fetchMyCarList(pageIndex = 1, pageSize = 20) {
  return new Promise((resolve) => {
    var url = "/car/user/stock/evaluated/pageData";
    var data = {
      pageNo: pageIndex,
      pageSize: pageSize,
      orderType: 'desc',
      orderField: 'id'
      // filter:[
      //   {field:'shopId',
      //   operator:'equal',value:}
      // ]
    }
    request.post(url, data, function (res) {
      if (res.code == 0) {
        resolve(res.data);
      } else {
        resolve([]);
      }
    })
  });
}

export function fetchAdminMyCarList(pageIndex = 1, pageSize = 20) {
  return new Promise((resolve) => {
    var url = "/car/admin/stock/pageData";
    var data = {
      pageNo: pageIndex,
      pageSize: pageSize,
      orderType: 'desc',
      orderField: 'id'
      // filter:[
      //   {field:'shopId',
      //   operator:'equal',value:}
      // ]
    }
    requestAdmin.post(url, data, function (res) {
      if (res.code == 0) {
        resolve(res.data);
      } else {
        resolve([]);
      }
    })
  });
}

export function fetchAdminMyBusiness(pageIndex = 1, pageSize = 20) {
  return new Promise((resolve) => {
    var url = "/car/admin/dealer/pageData";
    var data = {
      pageNo: pageIndex,
      pageSize: pageSize,
      orderType: 'desc',
      orderField: 'id'
      // filter:[
      //   {field:'shopId',
      //   operator:'equal',value:}
      // ]
    }
    requestAdmin.post(url, data, function (res) {
      if (res.code == 0) {
        resolve(res.data);
      } else {
        resolve([]);
      }
    })
  });
}